var express = require('express');
var router = express.Router();
var multer = require('multer');
var dir_name ="public/uploads/";
var upload = multer({dest:dir_name});
var fs = require('fs');
var conn = require("../setting");


//router中间件 ，解决后台页面的权限问题
/*router.use(function(req,res,next){
	 if (req.session.is_login==true) {
	 	next()
	 }else{
	 	res.redirect('/users/login')
	 }
	
})*/



router.get('/', function(req, res, next) {
	res.send("后台")
});

router.get('/addArticles', function(req, res, next) {
	
	conn.query('select * from catalog',function(err,rows,fields){
		if (err) throw err
		res.render("addArticles",{rows:rows})
	})
	

});

router.post('/addArticles',upload.single('images'), function(req, res, next) {
	/*文件上传*/
	/*fs.renameSync(dir_name+req.file.filename,dir_name+req.file.originalname)
	var imgPath = '/uploads/'+req.file.originalname
	console.log(imgPath)*/

	/*if (req.body.title=="") {
		req.session.error = "标题不能为空";
		res.redirect('addArticles')
	}else{*/
		if (req.body.title=="") {
			req.flash('title_error','标题不能为空');
        	res.redirect('addArticles');

		}else if(req.body.content==""){
			req.flash('content_error','内容不能为空');
        	res.redirect('addArticles');
		}else if(req.body.images==""){
			req.flash('image_error','图片不能为空');
        	res.redirect('addArticles');
		}else if(req.body.cata_id=="$"){
			req.flash('cataId_error','分类不能为空');
        	res.redirect('addArticles');
		}
		else{
			req.flash('success','添加文章成功');	
			var imgPath = req.body.images;
			var sql = "insert into articles values(null,'"+req.body.title+"','"+req.body.content+"','"+imgPath+"','"+req.body.cata_id+"')"
			conn.query(sql,function(err,rows,fields){
				
				
				res.redirect('/admin/articleLists')
			
			})
		}

	/*}*/


});



router.get('/articleLists',function(req,res,next){
			var sql = "select count(*) as count from articles";
			conn.query(sql,function(err,rows,fields){
				req.totle_article=rows[0].count;
				next();
			})
			
		  },function(req,res,next){

	/*var cookie_value = fs.readFileSync(req.cookies.cookie_id)*/ 
		if (req.query.page==undefined) {
			req.query.page=1;
		}
		var pageSize = 6;
		var page = (parseInt(req.query.page)-1)*pageSize;
		var totlePage = Math.ceil(req.totle_article/pageSize);
		console.log(req.query.page)
		
		var sql ="select * ,articles.id as aid from articles left join catalog on articles.cata_id=catalog.id order by aid desc limit "+page+","+pageSize
		conn.query(sql,function(err,rows,fields){
			if (err) throw err
			res.render("articleLists",{title:'文章列表',articles:rows,username:req.session.user_name,page:req.query.page,totlePage:totlePage})
		})

	

})


router.get('/delArticles', function(req, res, next) {
	var sql ="delete from articles where id="+req.query.id;
	conn.query(sql,function(err,rows,fields){
		 res.redirect('/admin/articleLists')	
		
	})

});




router.get('/reverse',function(req,res,next){
	conn.query("select * from catalog",function(err,rows,fields){
		var catalogs = rows;

	var sql = "select * from articles where id ="+req.query.id;

	conn.query(sql,function(err,rows,fields){
		
		res.render("reverse",{title:'修改文章',catalogs:catalogs,rows:rows[0]})
	})
	})
})



router.post('/reverse', function(req, res, next) {
	console.log(req.body)
	var sql = 'update articles set title="'+req.body.title+'",content="'+req.body.content+'",img="'+req.body.images+'",cata_id="'+req.body.cata_id+'" where id ='+ req.body.id
	console.log(sql)
	conn.query(sql,function(err,rows,fields){
		 res.redirect('/admin/articleLists')

		
	})

});


router.get('/exit', function(req, res, next) {
	req.session.destroy();
	res.redirect('/users/login')	
});



router.post('/uploadFile',upload.single('fileInput'),function(req,res,next){
	fs.renameSync(dir_name+req.file.filename,dir_name+req.file.originalname)
	var imgPath = '/uploads/'+req.file.originalname;
	res.send(imgPath);
	/*iframe+模态框上传文件*/
	/*res.send("<body onload =\"parent.changePath('"+imgPath+"')\"></body>");*/
})


router.get('/test', function(req, res, next) {
	res.send('123')	
});


module.exports = router;
